package sjtu.ist.warehouse4ws.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.json.annotations.JSON;



import sjtu.ist.warehouse4ws.model.CustModelgroup;
import sjtu.ist.warehouse4ws.model.CustRole;
import sjtu.ist.warehouse4ws.model.Domainmodel;
import sjtu.ist.warehouse4ws.model.Fileinfo;
import sjtu.ist.warehouse4ws.model.Functionpoint;
import sjtu.ist.warehouse4ws.model.Modelgroup;
import sjtu.ist.warehouse4ws.model.Process;
import sjtu.ist.warehouse4ws.model.Role;
import sjtu.ist.warehouse4ws.model.file.CompanyFileManager;
import sjtu.ist.warehouse4ws.model.file.CompanyFileManagerImplement;
import sjtu.ist.warehouse4ws.model.file.Suit;
import sjtu.ist.warehouse4ws.model.file.Version;
import sjtu.ist.warehouse4ws.model.file.VersionImp;

import sjtu.ist.warehouse4ws.service.DomainModelService;
import sjtu.ist.warehouse4ws.service.FileInfoService;
import sjtu.ist.warehouse4ws.service.FunctionpointService;
import sjtu.ist.warehouse4ws.service.ProcessService;
import sjtu.ist.warehouse4ws.service.ModelgroupService;
import sjtu.ist.warehouse4ws.service.cust.CustModelgroupService;
import sjtu.ist.warehouse4ws.vo.ModelgroupVO;

import com.opensymphony.xwork2.ActionSupport;

public class TreeAction extends ActionSupport {
	/**
	 * 
	 */
	private String nodeName=null;
	private String cid=null;
	
	private static final long serialVersionUID = 1180403231565653809L;
	private List <Domainmodel> domainModelList=new ArrayList<Domainmodel> ();
	private DomainModelService domainModelService;
	private String CnNameNode=null;
	
	private List<Process> processList=new ArrayList<Process>();
	private ProcessService processService;
	private String processNode=null;
	
	private List<Functionpoint> functionList=new ArrayList<Functionpoint>();
	private FunctionpointService functionpointService;
	private String functionNode=null;
	
	private List<ModelgroupVO> modelgroupList=new ArrayList<ModelgroupVO>();
	private ModelgroupService modelgroupService;
	private String modelgroupNode=null;

	private List<Fileinfo> fileinfoList=new ArrayList<Fileinfo>();
	private FileInfoService fileInfoService;
	private String versiongroupNode=null;
	
	private String viewgroupNode=null;

	private CustModelgroupService custModelgroupService;


	
	

	public void setDomainModelService(DomainModelService domainModelService) {
		this.domainModelService = domainModelService;
	}

	public void setProcessService(ProcessService processService){
		this.processService=processService;
	}
	
	public void setFunctionpointService(FunctionpointService functionpointService){
		this.functionpointService=functionpointService;
	}
	
	public void setModelgroupService(ModelgroupService modelgroupService) {
		this.modelgroupService = modelgroupService;
	}
	
	public void setFileInfoService(FileInfoService fileInfoService) {
		this.fileInfoService = fileInfoService;
	}

	public String showDomainTreeNode(){
		    this.domainModelList=domainModelService.getDomainModelList();
		    CnNameNode=showBusinessObject();
			return SUCCESS;								
			
			}
	
	public String showProcessTreeNode(){
		this.processList=processService.getProcessList();
		processNode=showProcess();
		return SUCCESS;
	}
	
	public String showFunctionTreeNode(){
		this.functionList=functionpointService.getFunctionpointList();
		functionNode=showFunction();
		return SUCCESS;
	}
	
//	public String showModelgroupTreeNode(){
//		modelgroupList=modelgroupService.getAll();
//		modelgroupNode=showModelgroup(modelgroupList);
//		return SUCCESS;
//	}
//	
//	public String showVersiongroupTreeNode(){
//		fileinfoList=fileInfoService.findFileInfoByProject(modelgroupList.get(0).getGroupName());
//		versionNode=showVersiongroup(fileinfoList);
//		return SUCCESS;
//	}
	
	public String showBusinessObject(){
		StringBuffer cnamejson = new StringBuffer();
		cnamejson.append("[");
		for(Domainmodel domain:domainModelList){		
			cnamejson.append("{text:'");
			cnamejson.append(domain.getCnName());
			cnamejson.append("'");
			cnamejson.append(",id:'");
			cnamejson.append(domain.getId());
			cnamejson.append("'");
			cnamejson.append(",leaf:true },");
			
		}
		cnamejson.deleteCharAt(cnamejson.length()-1);
		cnamejson.append("]");
	//	System.out.print(cnamejson.toString());
		return cnamejson.toString();
	}
	
	public String showProcess(){
		StringBuffer processjson = new StringBuffer();
		processjson.append("[");
		for(Process process:processList){
			processjson.append("{text:'");
			processjson.append(process.getCnName());
			processjson.append("'");
			processjson.append(",id:'");
			processjson.append(process.getId());
			processjson.append("'");
			processjson.append(",leaf:true },");
			
		}
		processjson.deleteCharAt(processjson.length()-1);
		processjson.append("]");
		System.out.print(processjson.toString());
		return processjson.toString();
	}
	
	public String showFunction(){
		StringBuffer processjson = new StringBuffer();
		processjson.append("[");
		for(Functionpoint function:functionList){
			processjson.append("{text:'");
			processjson.append(function.getCnName());
			processjson.append("'");
			processjson.append(",id:'");
			processjson.append(function.getId());
			processjson.append("'");
			processjson.append(",leaf:true },");
			
		}
		processjson.deleteCharAt(processjson.length()-1);
		processjson.append("]");
		System.out.print(processjson.toString());
		return processjson.toString();
	}
	
	//显示哪几套
	public String showModelgroup(){
	//	Subject currentUser = SecurityUtils.getSubject();
		
	//	String user_company =currentUser.getSession().getAttribute("companyId").toString();
	//	companyName=user_company;
		//MyUtils.mkDirectory(ServletActionContext.getServletContext().getRealPath("/upload/files/"));
	//	CompanyFileManager cfm=CompanyFileManagerImplement.getInstance(companyName,repositoryMoudlService);
		//System.out.println(cfm.getPath());
		
		modelgroupList = modelgroupService.getDistinctName();
		//System.out.println(modelgroupList.get(0).getGroupName());
		modelgroupNode=showTreeList();
		return SUCCESS;
	}
	
	public String showTreeList(){
		StringBuffer modelgroupjson = new StringBuffer();
		modelgroupjson.append("[");
		for(ModelgroupVO modelgroup:modelgroupList){
			modelgroupjson.append("{text:'");
			modelgroupjson.append(modelgroup.getGroupName());
			modelgroupjson.append("',");
			modelgroupjson.append("id:'");
			modelgroupjson.append(modelgroup.getId());
			modelgroupjson.append("',");
			modelgroupjson.append("leaf:false,");
			modelgroupjson.append("loader:new Ext.tree.JsonPluginTreeLoader({dataUrl:");
			modelgroupjson.append("'showVersiongroup?nodeName="+modelgroup.getGroupName()+"'})");
			//processjson.append(",listener:{'click' : function(node) {var i=n.parentNode.id}");
			modelgroupjson.append("},");			
		}
		modelgroupjson.deleteCharAt(modelgroupjson.length()-1);
		modelgroupjson.append("]");
	
		return modelgroupjson.toString();
	}
	
	public String showModelgroupList(){	
		Subject currentUser = SecurityUtils.getSubject();
		Integer company=(Integer) currentUser.getSession().getAttribute("companyId");
		
		//ActionContext actionContext=ActionContext.getContext();
		//Map session=actionContext.getSession();		
		//int userType=(Integer)(session.get("user_type"));
		//String userCompany=session.get("user_company").toString();	
		
		if(!currentUser.hasRole("superadmin")){
//			this.modelgroupList=new ArrayList<Modelgroup>();
//			List<CustModelgroup> templist=custModelgroupService.getByName(company.toString());
//			for(CustModelgroup r : templist){
//			modelgroupList.add(r);
//			}
		}
		else{
			modelgroupList = modelgroupService.getDistinctName();
		}		
		return SUCCESS;
	}
	@JSON(name = "totalModelgroup")
	public int getTotalRole() {
		return this.modelgroupList.size();
	}
	@JSON(name = "Modelgroup")
	public List<ModelgroupVO> getModelgroupList() {
		return this.modelgroupList;
	}
	
	//显示模型的版本层
	public String showModelgroupContent()
	{
		StringBuffer versiongroupjson = new StringBuffer();
		versiongroupjson.append("[");

		//CompanyFileManager cfm=CompanyFileManagerImplement.getInstance(companyName,repositoryMoudlService);
		fileinfoList = fileInfoService.findFileInfoByProject(nodeName);
		
		for(Fileinfo fileinfo:fileinfoList){
			versiongroupjson.append("{text:'");
			versiongroupjson.append(fileinfo.getVersionId());
			versiongroupjson.append("'");
			versiongroupjson.append(",leaf:false,");
			versiongroupjson.append("loader:new Ext.tree.JsonPluginTreeLoader({dataUrl:");
			versiongroupjson.append("'showViewgroup?nodeName="+fileinfo.getVersionId()+"'})");
			//processjson.append(",listener:{'click' : function(node) {var i=n.parentNode.id}");
			versiongroupjson.append("},");
			
			
		}
		versiongroupjson.deleteCharAt(versiongroupjson.length()-1);
		versiongroupjson.append("]");
	
		versiongroupNode=versiongroupjson.toString();
		
		return SUCCESS;
	}
	public String showVersiongroupList(){	
		Subject currentUser = SecurityUtils.getSubject();
		Integer company=(Integer) currentUser.getSession().getAttribute("companyId");
		
		//ActionContext actionContext=ActionContext.getContext();
		//Map session=actionContext.getSession();		
		//int userType=(Integer)(session.get("user_type"));
		//String userCompany=session.get("user_company").toString();	
		
		if(!currentUser.hasRole("superadmin")){
//			this.fileinfoList=new ArrayList<Fileinfo>();
//			List<CustFileinfo> templist=custFileinfoService.getByName(company.toString());
//			for(CustFileinfo r : templist){
//			fileinfoList.add(r);
//			}
		}
		else{
			fileinfoList = fileInfoService.findFileInfoByProject(cid);
		}	
//		System.out.println(fileinfoList.get(0).getProject());
		return SUCCESS;
	}
	@JSON(name = "totalVersiongroup")
	public int getTotalVersion() {
		return this.fileinfoList.size();
	}
	@JSON(name = "Versiongroup")
	public List<Fileinfo> getVersiongroupList() {
		return this.fileinfoList;
	}
	
	//显示模型的视图层
	public String showVersiongroupContent()
	{
		StringBuffer viewgroupjson = new StringBuffer();
		viewgroupjson.append("[");

		//CompanyFileManager cfm=CompanyFileManagerImplement.getInstance(companyName,repositoryMoudlService);
		//fileinfoList = fileInfoService.findFileInfoByProject(nodeName);
		
		//for(Fileinfo fileinfo:fileinfoList){
			viewgroupjson.append("{id:'functiongroup',text:'功能视图',");
			viewgroupjson.append("children:[{");
			viewgroupjson.append("id:'processlist',");
			viewgroupjson.append("text:'功能结构',");
			viewgroupjson.append("leaf:true},");
			viewgroupjson.append("{id:'functionpointlist',");
			viewgroupjson.append("text:'功能库',");
			viewgroupjson.append("leaf:true}]},");
			
			viewgroupjson.append("{id:'organizationgroup',text:'组织视图',");
			viewgroupjson.append("children:[{");
			viewgroupjson.append("id:'companylist',");
			viewgroupjson.append("text:'组织结构',");
			viewgroupjson.append("leaf:true},");
			viewgroupjson.append("{id:'rolelist',");
			viewgroupjson.append("text:'角色权限',");
			viewgroupjson.append("leaf:true}]},");

			viewgroupjson.append("{text:'业务流程视图',");
			viewgroupjson.append("id:'epclist',");
			viewgroupjson.append("leaf:true},");
			
			viewgroupjson.append("{text:'业务对象视图',");
			viewgroupjson.append("id:'domainlist',");
			viewgroupjson.append("leaf:true},");
		//}
		viewgroupjson.deleteCharAt(viewgroupjson.length()-1);
		viewgroupjson.append("]");
	
		viewgroupNode=viewgroupjson.toString();
		
		return SUCCESS;
	}
//	//显示每个版本下的那些文件和文件夹           project这里改
//	public String showVersionContent(){
//		StringBuffer viewgroupjson = new StringBuffer();
//		viewgroupjson.append("[");
//		Version version=new VersionImp(nodeName);
//		ArrayList<sjtu.ist.warehouse4ws.model.file.View> views=version.getViews(repositoryMoudlService);
//		for(sjtu.ist.warehouse4ws.model.file.View view:views){
//		viewgroupjson.append(showNode(view.getName(),view.getName(),view.getPath()));
//		}
//		viewgroupjson.deleteCharAt(viewgroupjson.length()-1);
//		viewgroupjson.append("]");
//		viewgroupNode=viewgroupjson.toString();
//		return SUCCESS;
//	}
//	public String showNode(String text,String id,String link){
//		StringBuffer processjson = new StringBuffer();
//		if(text.contains(".")){
//			processjson.append(" {text : \"");
//			processjson.append(text);
//			processjson.append("\",id : '");
//			processjson.append(id);
//			processjson.append("',leaf:true");
//			processjson.append("}, ");
//		}else{
//			processjson.append(" {text : \"");
//			processjson.append(text);
//			processjson.append("\",id : '");
//			processjson.append(id);
//			processjson.append("',leaf:false,loader : new Ext.tree.JsonPluginTreeLoader({dataUrl : '");
//			processjson.append("showViewContent");
//			processjson.append("?nodeName="+link+"'})}, ");
//		}
//	
//		
//		String str=processjson.toString();
//		return str;
//	}
//	public String showModelgroup(List<Modelgroup> modelgroupList){
//		StringBuffer modelgroupjson = new StringBuffer();
//		modelgroupjson.append("[");
//		for(Modelgroup modelgroup:modelgroupList){
//			modelgroupjson.append("{text:'");
//			modelgroupjson.append(modelgroup.getGroupName());
//			modelgroupjson.append("'");
//			modelgroupjson.append(",id:'");
//			modelgroupjson.append(modelgroup.getId());
//			modelgroupjson.append("'");
//			modelgroupjson.append(",leaf:false,");
//			modelgroupjson.append("loader:new Ext.tree.JsonPluginTreeLoader({dataUrl:");
//			modelgroupjson.append("'showVersiongroup?nodeName="+modelgroup.getGroupName()+"'})");
//			modelgroupjson.append("},");
//			
//		}
//		modelgroupjson.deleteCharAt(modelgroupjson.length()-1);
//		modelgroupjson.append("]");
//		System.out.print(modelgroupjson.toString());
//		return modelgroupjson.toString();
//	}
//	
//	public String showVersiongroup(List<Fileinfo> fileinfoList){
//		StringBuffer versionjson = new StringBuffer();
//		versionjson.append("[");
//		for(Fileinfo fileinfo:fileinfoList){
//			versionjson.append("{text:'");
//			versionjson.append(fileinfo.getVersionId());
//			versionjson.append("'");
//			versionjson.append(",id:'");
//			versionjson.append(fileinfo.getId());
//			versionjson.append("'");
//			versionjson.append(",leaf:true },");
//			
//		}
//		versionjson.deleteCharAt(versionjson.length()-1);
//		versionjson.append("]");
//		System.out.print(versionjson.toString());
//		return versionjson.toString();
//	}
	////////////////////////////////
	public String getCnNameNode() {
		return CnNameNode;
	}

	public void setCnNameNode(String cnNameNode) {
		CnNameNode = cnNameNode;
	}

	public String getProcessNode() {
		return processNode;
	}

	public void setProcessNode(String processNode) {
		this.processNode = processNode;
	}

	public String getFunctionNode() {
		return functionNode;
	}

	public void setFunctionNode(String functionNode) {
		this.functionNode = functionNode;
	}

	public String getNodeName() {
		return nodeName;
	}

	public void setNodeName(String nodeName) {
		this.nodeName = nodeName;
	}

	public String getModelgroupNode() {
		return modelgroupNode;
	}

	public void setModelgroupNode(String modelgroupNode) {
		this.modelgroupNode = modelgroupNode;
	}

	public String getVersiongroupNode() {
		return versiongroupNode;
	}

	public void setVersiongroupNode(String versiongroupNode) {
		this.versiongroupNode = versiongroupNode;
	}

	public String getViewgroupNode() {
		return viewgroupNode;
	}

	public void setViewgroupNode(String viewgroupNode) {
		this.viewgroupNode = viewgroupNode;
	}

	public CustModelgroupService getCustModelgroupService() {
		return custModelgroupService;
	}

	public void setCustModelgroupService(CustModelgroupService custModelgroupService) {
		this.custModelgroupService = custModelgroupService;
	}

	public String getCid() {
		return cid;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}


	
	
	
	

	/////////////////////////////////////////
    
    

}
